package com.ramotion.expandingcollection;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.PageFlipper;
import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * FeatureAbility
 *
 * @since 2021-05-31
 */
public class ItemsCountView extends DirectionalLayout {
    private static final int TEXT_SIZE = 180;
    private static final int DURATION = 400;
    private static final float SIZE = 0.75f;
    private PageFlipper textSwitcher;
    private Text textView;

    /**
     * ItemsCountView
     *
     * @param context
     */
    public ItemsCountView(Context context) {
        super(context);
        init(context);
    }

    /**
     * ItemsCountView
     *
     * @param context
     * @param attrSet
     */
    public ItemsCountView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context);
    }

    private void init(Context context) {
        setClipEnabled(false);
        setOrientation(ComponentContainer.HORIZONTAL);
        textSwitcher = new PageFlipper(context);
        textSwitcher.addComponent(createViewForTextSwitcher(context));
        textSwitcher.addComponent(createViewForTextSwitcher(context));
        textSwitcher.addComponent(createViewForTextSwitcher(context));
        textSwitcher.addComponent(createViewForTextSwitcher(context));
        addComponent(textSwitcher, new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT));
        textView = new Text(context);
        textView.setTextSize(TEXT_SIZE);
        textView.setFont(Font.DEFAULT_BOLD);
        textView.setTextColor(new Color(Color.getIntColor("#8cffffff")));
        addComponent(textView, new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT));
    }

    private Text createViewForTextSwitcher(Context context) {
        Text text = new Text(context);
        text.setTextSize(TEXT_SIZE);
        text.setFont(Font.DEFAULT_BOLD);
        text.setTextColor(Color.WHITE);
        text.setLayoutConfig(new PageFlipper.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
                PageFlipper.LayoutConfig.MATCH_CONTENT));
        return text;
    }

    /**
     * update
     *
     * @param newPosition
     * @param oldPosition
     * @param totalElements
     */
    public void update(int newPosition, int oldPosition, int totalElements) {
        textView.setText(" / " + totalElements);
        int offset = (int) (textSwitcher.getHeight() * SIZE);
        if (newPosition > oldPosition) {
            textSwitcher.setIncomingAnimation(createPositionAnimation(-offset, 0, 0f, 1f, DURATION));
            textSwitcher.setOutgoingAnimation(createPositionAnimation(0, offset, 1f, 0f, DURATION));
        } else if (oldPosition > newPosition) {
            textSwitcher.setIncomingAnimation(createPositionAnimation(offset, 0, 0f, 1f, DURATION));
            textSwitcher.setOutgoingAnimation(createPositionAnimation(0, -offset, 1f, 0f, DURATION));
        }
        textSwitcher.showNext();
        Text text = (Text) textSwitcher.getCurrentComponent();
        text.setText(String.valueOf(newPosition + 1));
    }

    private AnimatorProperty createPositionAnimation(int fromY, int toY, float fromAlpha, float toAlpha, int duration) {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty.setCurveType(Animator.CurveType.DECELERATE);
        animatorProperty.alphaFrom(fromAlpha);
        animatorProperty.alpha(toAlpha);
        animatorProperty.moveFromY(fromY);
        animatorProperty.moveToY(toY);
        animatorProperty.setDuration(duration);
        return animatorProperty;
    }
}
